package com.ruoyi.tbxt.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.tbxt.domain.BizSystem;
import com.ruoyi.tbxt.service.IBizSysService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/biz/sys")
public class BizSystemController extends BaseController {
    private String prefix = "biz/sys";

    @Autowired
    private IBizSysService bizSysService;

    @RequiresPermissions("biz:sys:view")
    @GetMapping()
    public String sys()
    {
        return prefix + "/sys";
    }

    @RequiresPermissions("biz:sys:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizSystem biz)
    {
        startPage();
        List<BizSystem> list = bizSysService.selectBizSysList(biz);
        return getDataTable(list);
    }

    /**
     * 新增接入系统
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增Action
     */
    @RequiresPermissions("biz:sys:add")
    @Log(title = "接入系统管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated BizSystem biz)
    {
        if (!bizSysService.checkBizCodeUnique(biz.getBizCode()))
        {
            return error("新增接入系统'" + biz.getBizName() + "'失败，系统编号已存在");
        }

        return toAjax(bizSysService.insertBiz(biz));
    }

    /**
     * 修改接入业务系统
     */
    @RequiresPermissions("biz:sys:edit")
    @GetMapping("/edit/{bizId}")
    public String edit(@PathVariable("bizId") Long bizId, ModelMap mmap)
    {
//        bizSysService.checkUserDataScope(userId);
//        userService.checkUserDataScope(userId);
        mmap.put("bizSys", bizSysService.selectBizById(bizId));
//        List<SysRole> roles = roleService.selectRolesByUserId(userId);
//        mmap.put("user", userService.selectUserById(userId));
//        mmap.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
//        mmap.put("posts", postService.selectPostsByUserId(userId));
        return prefix + "/edit";
    }

    /**
     * 修改保存接入业务系统
     */
    @RequiresPermissions("biz:sys:edit")
    @Log(title = "接入系统管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated BizSystem biz)
    {
        if (!bizSysService.checkBizCodeUnique(biz.getBizCode(), biz.getBizId()))
        {
            return error("修改接入系统'" + biz.getBizName() + "'失败，系统编号已存在");
        }
        biz.setUpdateBy(getLoginName());
        return toAjax(bizSysService.updateBiz(biz));
    }

    /**
     * 用户状态修改
     */
    @Log(title = "接入系统管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("biz:sys:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(BizSystem biz)
    {
        return toAjax(bizSysService.updateBiz(biz));
    }

    /**
     * 查询详细信息
     */
    @RequiresPermissions("biz:sys:list")
    @GetMapping("/view/{bizId}")
    public String view(@PathVariable("bizId") Long bizId, ModelMap mmap)
    {
        mmap.put("bizSys", bizSysService.deleteBizById(bizId));
        return prefix + "/view";
    }

    @RequiresPermissions("biz:sys:remove")
    @Log(title = "接入系统管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bizSysService.deleteBizByIds(ids));
    }

}
